<?php

/***
 * 描述：订单模型
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: OrderModel.class.php 12 2013-10-22 08:31:42Z whtaok@gmail.com $
 */
class OrderModel {
	/**
	 * 描述：生成订单（做事务处理）
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		array $orderData  订单信息
	 * @param 		array $orderDetailData	订单商品信息
	 * @since		2013-4-21
	 */
	public static function addOrder($orderData,$orderDetailData){
		$tranFlag = true;
		$orderMod = new CommonModel('OrderLib');
		$orderMod->startTrans();
		$lastId = $orderMod->add($orderData);
		if($lastId){
			$orderDetailMod = new CommonModel('OrderDetail');
			foreach ($orderDetailData as $key => $value){
				if($orderDetailMod->add($value)){
					//库存操作移出该方法
				}else{
					$tranFlag = false;
					break;
				}
			}
		}else{
			$tranFlag = false;
		}
		if($tranFlag){
			$orderMod->commit();
		}else{
			$orderMod->rollback();
		}
		return $tranFlag;
	}
	
	/**
	 * 描述：进行中订单，历史订单查询
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $cust_id	客户ID
	 * @param 		array  $search	查询条件
	 * @param 		string $order	排序
	 * @since		2013-4-21
	 */
	public static function findOrderListPage($cust_id,$search=array(),$order='id desc'){
		$modWhere = array(
			'cust_id'=>$cust_id,
			'is_show'=>1					//显示订单
		);
		if($search['is_history']=='1'){
			$modWhere['order_status']=array('in',array(0,4));	//已完成订单
		}else{
			$modWhere['order_status']=array('in',array(1,2,3));	//处理中的订单
		}
		$mod = new CommonModel('OrderLib');
		$totalRows = $mod->where($modWhere)->count();
		list($list,$page) = $mod->where($modWhere)->order($order)->listPage($totalRows);
		foreach($list as $key => $value){
			switch ($value['cust_order_status']){
				case 0:
					$list[$key]['cust_order_status_text']='';
					break;
				case 1:
					$list[$key]['cust_order_status_text']='部分退货';
					break;
				case 2:
					$list[$key]['cust_order_status_text']='全部退货';
					break;
				case 3:
					$list[$key]['cust_order_status_text']='订单取消';
					break;
			}
			if($value['order_status']==0 || $value['cust_order_status']==2 || $value['cust_order_status']==3){
				//订单完成，显示客户支付金额
				$list[$key]['order_amt'] = $value['cust_pay_amt'];
			}else{
				//否则显示订单金额+运费金额
				$list[$key]['order_amt'] += $value['order_send_fee'];
			}
		}
		return array($list,$page);
	}
	
	/**
	 * 描述：订单详细信息查询
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string $cust_id		客户ID
	 * @param 		string $order_code	订单号
	 * @since		2013-4-21
	 */
	public static function findOrderDetail($cust_id,$order_code){
		$modWhere = array(
				'cust_id'=>$cust_id,			//客户ID
				'order_code'=>$order_code,		//订单号
				'is_show'=>1,					//显示订单
		);
		$mod = new CommonModel('OrderLib');			//订单信息
		$order = $mod->where($modWhere)->find();
		switch ($order['cust_order_status']){
				case 1:
					$order['cust_order_status_text']='';
					break;
				case 2:
					$order['cust_order_status_text']='订单取消';
					break;
				case 3:
					$order['cust_order_status_text']='部分退货';
					break;
		}
		if($order['order_status']==0  || $order['cust_order_status']==2 || $order['cust_order_status']==3){
			//订单完成，显示客户支付金额
			$order['order_amt'] = $order['cust_pay_amt'];
		}else{
			//否则显示订单金额+运费金额
			$order['order_amt'] += $order['order_send_fee'];
		}
		$mod = new CommonModel('OrderDetail');		//订单商品列表
		$itmes = $mod->where($modWhere)->select();
		//核算退货后的商品总价格
		$prod_amt = 0;
		foreach($itmes as $key=>$value){
			
			$prod_amt += ($value['prod_num']-$value['prod_cancel_num'])*$value['prod_price_real'];
			$value['prod_spec'] = m_prod_spec($value['prod_spec'],$value['prod_spec_qty'],$value['prod_spec_unit']); //规格
			//图片
			if($order['order_type']==1){
				$value['prod_pic'] = m_prod_pic($value['prod_id']);
			}else if($order['order_type']==2){
				$value['prod_pic'] = m_prod_pic($value['prod_id'],'tg_prod');
				$value['prod_link'] = U('Tuangou/showDetail',array('prod_id'=>$value['prod_id']));	//链接
			}
			$itmes[$key]=$value;
		}
		$order['prod_amt'] = $prod_amt;
		$rtnData = array($order,$itmes);
		return $rtnData;
	}
	
}

?>